package com.jackaltsc.mydemodatastorage.contentprovider;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.jackaltsc.mydemodatastorage.R;


public class ContentProvicerActivity extends Activity implements OnClickListener {

    private Button btnInsert, btnDelete, btnQuery, btnUpdate;
    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_contentprovider);

        initView();

    }

    private void initView() {

        btnInsert = (Button) findViewById(R.id.btn_cv_insert);
        btnDelete = (Button) findViewById(R.id.btn_cv_delete);
        btnQuery = (Button) findViewById(R.id.btn_cv_query);
        btnUpdate = (Button) findViewById(R.id.btn_cv_update);
        tvResult = (TextView) findViewById(R.id.tv_cv_result);

        initEvent();
    }

    private void initEvent() {

        btnInsert.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnQuery.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btn_cv_insert:
                cvInsert();
                break;

            case R.id.btn_cv_delete:
                cvDelete();
                break;

            case R.id.btn_cv_query:
                cvQuery();
                break;

            case R.id.btn_cv_update:
                cvUpdate();
                break;
        }

    }

    private void cvUpdate() {

        ContentResolver cr = this.getContentResolver();

        Uri url = Uri
                .parse("content://com.jackaltsc.mydemodatastorage.contentprovider.MyContentProvider/"
                        + MySQLiteHelper.TABLEC_NAME + "/4");

        ContentValues cv = new ContentValues();
        cv.put("name", "name更新");
        cv.put("address", "address更新");

        if (cr.update(url, cv, null, null) != -1) {
            Toast.makeText(this, "更新成功！", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "更新失败！", Toast.LENGTH_SHORT).show();
        }
    }

    private void cvQuery() {

        ContentResolver cr = this.getContentResolver();

        Uri url = Uri
                .parse("content://com.jackaltsc.mydemodatastorage.contentprovider.MyContentProvider/"
                        + MySQLiteHelper.TABLEC_NAME + "/1");
        Cursor cursor = cr.query(url, null, null, null, null);

        while (cursor.moveToNext()) {
            tvResult.setText(cursor.getString(cursor.getColumnIndex("name")));
        }

        cursor.close();
    }

    private void cvDelete() {

        ContentResolver cr = this.getContentResolver();
        Uri url = Uri
                .parse("content://com.jackaltsc.mydemodatastorage.contentprovider.MyContentProvider/" +
                        MySQLiteHelper.TABLEC_NAME + "/8");
        if (cr.delete(url, null, null) == -1) {
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void cvInsert() {

        ContentResolver cr = this.getContentResolver();

        Uri url = Uri
                .parse("content://com.jackaltsc.mydemodatastorage.contentprovider.MyContentProvider/"
                        + MySQLiteHelper.TABLEC_NAME);

        ContentValues cv = new ContentValues();
        cv.put("name", "testwww");
        cv.put("address", "浙江杭州");

        String result = cr.insert(url, cv).toString();

        if (result == null) {
            result = "添加失败！";
        }

        tvResult.setText(result);
    }
}
